package com.cssw.fyzb.busi.api.exchange;

import com.cssw.fyzb.busi.api.user.ListFans;
import com.cssw.fyzb.busi.service.exchange.GExchangeFService;
import com.cssw.fyzb.core.exception.BizException;
import com.cssw.fyzb.core.model.vo.exchang.GExchangeFVo;
import com.ydn.appserver.Action;
import com.ydn.appserver.Request;
import com.ydn.appserver.Response;
import com.ydn.appserver.annotations.*;
import com.ydn.simplelock.SimpleLock;
import com.ydn.simplelock.SimpleLockFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;

@Function(description = "果子兑换福币", parameters = {
        @Parameter(name = "userId", type = Type.Integer, description = "用户标识", required = false),
        @Parameter(name = "num", type = Type.Integer, description = "数量", required = false),
        @Parameter(name = "logId", type = Type.Integer, description = "果子表列表id", required = false),
        @Parameter(name = "type", type = Type.Integer, description = "音值表列表id", required = true),
        @Parameter(name = "pwd", type = Type.String, description = "交易密码", required = true),
})

@Component
public class GExchangeF implements Action {
    @Autowired
    private GExchangeFService gExchangfService;
    private static final Logger logger = LoggerFactory.getLogger(ListFans.class);

    @Autowired
    private SimpleLockFactory simpleLockFactory;

    @Override
    public Response execute(Request request) throws Exception {
        if (true){
            return Response.fail("兑换功能暂未开放");
        }

        Long userId = request.getLong("userId");
        Double num = request.getDouble("num");
        String pwd = request.getString("pwd");
        Long logId = request.getLong("logId");
        Integer type = request.getInteger("type");
        SimpleLock lock = simpleLockFactory.getLock("USER_" + userId);
        try {
            lock.acquire();
            if (logId == null) logId = 0l;
            String result = gExchangfService.gExchangeFDef(new BigDecimal(num), userId, logId, type, pwd);
            GExchangeFVo vo = new GExchangeFVo();
            vo.setFbValue(result);
            return Response.success().put("data", vo);
        } catch (Exception e) {
            if (e instanceof BizException) {
                BizException be = (BizException) e;
                return Response.fail(be.getErrMsg(), be.getErrCode());
            }
            logger.info("", e);
            return Response.fail("操作失败");
        } finally {
            lock.release();
        }
    }

}
